#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "MSTK.h"


int main(int argc, char *argv[]) {
  int k, len, suff, ok;
  char mname[256], filename[256];
  Mesh_ptr mesh;


  if (argc == 1) {
    fprintf(stdout, "pmstk2gmv converts a mstk mesh into gmv format with ptype and masterparid information as attribute\n");
    fprintf(stdout, "Usage: pmstk2gmv filename\n");
    return -1;
  }

  fprintf(stderr,"\n");


  /* Initialize MSTK - Always do this even if it does not seem to
     matter in this version of MSTK */

  MSTK_Init();

  /* Load the mesh */

  strcpy(mname,argv[1]);
  len = strlen(mname);
  suff = 0;
  if (len > 5) {
    k = len-5;
    while (!suff && k > 1) {
      if (strncmp(&(mname[k]),".mstk",5) == 0) {
        suff = 1;
      }
      else
        k--;
    }
  }
  if (suff)
    mname[len-5] = '\0';

  strcpy(filename,mname);
  strcat(filename,".mstk");

  mesh = MESH_New(UNKNOWN_REP);
  ok = PMESH_InitFromFile(mesh,filename);
  if (!ok) {
    fprintf(stderr,"Cannot open input file %s\n\n\n",filename);
    exit(-1);
  }

  /* make ptype ,master par id and global id as attribute */
  int idx;
  MAttrib_ptr vertex_ptype = MAttrib_New(mesh, "vertex_ptype", INT, MVERTEX);
  MAttrib_ptr vertex_marparid = MAttrib_New(mesh, "vertex_marparid", INT, MVERTEX);
  MAttrib_ptr vertex_globalid = MAttrib_New(mesh, "vertex_globalid", INT, MVERTEX);
  idx = 0;
  MVertex_ptr local_mv;
  while((local_mv = MESH_Next_Vertex(mesh,&idx))) {
    MEnt_Set_AttVal(local_mv,vertex_ptype,MV_PType(local_mv),0,NULL);
    MEnt_Set_AttVal(local_mv,vertex_marparid,MV_MasterParID(local_mv),0,NULL);
    MEnt_Set_AttVal(local_mv,vertex_globalid,MV_GlobalID(local_mv),0,NULL);
  }

  if(MESH_Num_Regions(mesh)) {
    MRegion_ptr local_mr;
    MAttrib_ptr region_ptype = MAttrib_New(mesh, "region_ptype", INT, MREGION);
    MAttrib_ptr region_marparid = MAttrib_New(mesh, "region_marparid", INT, MREGION);
    MAttrib_ptr region_globalid = MAttrib_New(mesh, "region_globalid", INT, MREGION);
    idx = 0;
    while((local_mr = MESH_Next_Region(mesh,&idx))) {
      MEnt_Set_AttVal(local_mr,region_ptype,MR_PType(local_mr),0,NULL);
      MEnt_Set_AttVal(local_mr,region_marparid,MR_MasterParID(local_mr),0,NULL);
      MEnt_Set_AttVal(local_mr,region_globalid,MR_GlobalID(local_mr),0,NULL);
    }

  }
  else {
    MFace_ptr local_mf;
    MAttrib_ptr face_ptype = MAttrib_New(mesh, "face_ptype", INT, MFACE);
    MAttrib_ptr face_marparid = MAttrib_New(mesh, "face_marparid", INT, MFACE);
    MAttrib_ptr face_globalid = MAttrib_New(mesh, "face_globalid", INT, MFACE);
    idx = 0;
    while((local_mf = MESH_Next_Face(mesh,&idx))) {
      MEnt_Set_AttVal(local_mf,face_ptype,MF_PType(local_mf),0,NULL);
      MEnt_Set_AttVal(local_mf,face_marparid,MF_MasterParID(local_mf),0,NULL);
      MEnt_Set_AttVal(local_mf,face_globalid,MF_GlobalID(local_mf),0,NULL);
    }
  }

  strcpy(filename,mname);
  strcat(filename,".gmv");
  MESH_ExportToGMV(mesh,filename,0,NULL,NULL);
  fprintf(stdout,"gmv file %s has been generated\n",filename);
  
  /* Deleting of mesh is not necessary if this the end of the program */

  MESH_Delete(mesh);

  return 1;
}
